package com.example.ximalaya;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.ximalaya.adapters.IndicatorAdapter;
import com.example.ximalaya.adapters.MainContentAdapter;
import com.example.ximalaya.utils.LogUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

public class MainActivity extends FragmentActivity {
public static final String TAG="MainActivity";
    private MagicIndicator mMagicIndicator;
    private ViewPager mContentPager;
    private IndicatorAdapter mIndicatorAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        makeStatusBarTransparent(this);
        initView();
        initEvent();


    }

    private void initEvent() {
       mIndicatorAdapter.setOnIndicatorTapClickListener(new IndicatorAdapter.OnIndicatorTapClickListener() {
           //匿名内部类，通过方法参数里new接口实现方法，从而实现回调
           @Override
           public void onTabClick(int index) {
               LogUtil.d(TAG,"Click index is--"+index);
               if(mContentPager!=null){
                   mContentPager.setCurrentItem(index);
               }
           }
       });
    }

    private void initView() {
        //整个框架的入口，核心
        mMagicIndicator = this.findViewById(R.id.main_indicator);
        mMagicIndicator.setBackgroundColor(this.getResources().getColor(R.color.main_color));
        //创建indicator适配器(用来显示滑动的标题和滑动时候的下划线)
        mIndicatorAdapter = new IndicatorAdapter(this);
        //CommonNavigator为viewpager的指示器(上面部分的滑动)包含PagerTitle和PagerIndicator
        CommonNavigator commonNavigator = new CommonNavigator(this);
        //指示器自动调整间距(平方指示器的间距)
        commonNavigator.setAdjustMode(true);
        //给指示器设置适配器
        commonNavigator.setAdapter(mIndicatorAdapter);

        //ViewPager
        mContentPager = this.findViewById(R.id.content_Pager);

        //使得滑动页面内容是fragment的内容
        //获取FragmentManager在活动中可以直接通过调用getSupportFragmentManager()获取FragmentManager对象
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        MainContentAdapter mainContentAdapter=new MainContentAdapter(supportFragmentManager);
        mContentPager.setAdapter(mainContentAdapter);

        //把ViewPager和indicator绑定到一起（进行关联，当一个变化，会引起另一个的变化）通过监听
        //将commNavigator设置给mMagicIndicator,不然没有导航栏没有显示
        mMagicIndicator.setNavigator(commonNavigator);
        //最后的目的使得滑动页面的时候，上面(的指示器和页面)一起联动
        ViewPagerHelper.bind(mMagicIndicator, mContentPager);

    }


    //显示透明栏·
    public static void makeStatusBarTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            window.getDecorView().setSystemUiVisibility(option);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
}